﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using MbUnit.Framework;
using System.Threading;

namespace HelloMVC.Test.Selenium
{
    [TestFixture]
    public class IssuesNavigatorPage
    {
        private IWebDriver Browser { get; set; }

        [TestFixtureSetUp]
        public void SetUp()
        {
            Browser = new FirefoxDriver();
            Browser.Navigate().GoToUrl("http://localhost/NIRA_DEV/DashBoard/Issue.mvc");
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            Browser.Close();
        }

        [Test("Navigator options: Views")]
        public void TestViewsOptions()
        {
            IWebElement viewOptions = Browser.FindElement(By.Id("viewsOptions"));
            IWebElement viewOptionsText = viewOptions.FindElement(By.XPath("span"));
            Assert.AreEqual("Views", viewOptionsText.Text);
            viewOptions.Click();
            Thread.Sleep(500);
            IWebElement dropContent = Browser.FindElement(By.Id("viewOptions_drop"));
            var dropdownItems = dropContent.FindElements(By.XPath("div/ul[*]/li[*]/a"));
            Assert.AreEqual(12, dropdownItems.Count);
            var dropdownItemText = new[] { 
                "Printable", "Full Content", "XML", "RSS(Issues)", "RSS(Comment)",
                "Word", "Excel(All fields)", "Excel(Current fields)", "Charts",
                "On Dashboard", "PDF (Scrum Cards sheet)", "PDF (Scrum Cards single)"
            };

            for (Int32 i = 0; i < dropdownItemText.Length; i++)
            {
                Assert.AreEqual(dropdownItemText[i], dropdownItems[i].Text);
            }
        }

        [Test("Navigator options: Tools")]
        public void TestToolsOptions()
        {
            IWebElement toolOptions = Browser.FindElement(By.Id("toolsOptions"));
            IWebElement toolOptionsText = toolOptions.FindElement(By.XPath("span"));
            Assert.AreEqual("Tools", toolOptionsText.Text);
            toolOptions.Click();
            Thread.Sleep(500);
            IWebElement dropContent = Browser.FindElement(By.Id("toolsOptions_drop"));
            var dropdownItems = dropContent.FindElements(By.XPath("div/ul[*]/li[*]/a"));
            Assert.AreEqual(1, dropdownItems.Count);
            var dropdownItemText = new[] { "Configure Columns" };

            for (Int32 i = 0; i < dropdownItemText.Length; i++)
            {
                Assert.AreEqual(dropdownItemText[i], dropdownItems[i].Text);
            }
        }

        [Test("Browse issues: data table, verify column headers")]
        public void TestIssuesTable()
        {
            var dataTable = Browser.FindElement(By.Id("example"));
            var columnHeaders = dataTable.FindElements(By.XPath("thead/tr/th"));
            var columnHeaderTexts = new[] { 
                "T", "Key", "Summary", "Assignee", "Reporter", "P", "Status", "Resolution", "Created", "Updated"
            };

            Assert.AreEqual(columnHeaderTexts.Length, columnHeaders.Count);

            for (Int32 i = 0; i < columnHeaderTexts.Length; i++)
            {
                Assert.AreEqual(columnHeaderTexts[i], columnHeaders[i].Text);
            }
        }

        [Test("Browse issues: data table, verify records count, datatable information")]
        public void TestIssuesCount()
        {
            var warpper = Browser.FindElement(By.Id("example_wrapper"));
            var dataTable = Browser.FindElement(By.Id("example"));
            var records = dataTable.FindElements(By.XPath("tbody/tr"));
            var datatableInfo = warpper.FindElements(By.ClassName("dataTables_info"));
            Assert.AreEqual(50, records.Count);
            Assert.AreEqual(2, datatableInfo.Count);
            Assert.AreEqual("Displaying issues 1 to 50 of 126 matching issues.", datatableInfo[0].Text);
            Assert.AreEqual("Displaying issues 1 to 50 of 126 matching issues.", datatableInfo[1].Text);
        }

    }
}
